<?php

/**
 * 举报
 */
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\facade\Db;

class ChannelController extends AdminbaseController {

	function index(){
        $data = $this->request->param();
        $lists = Db::name("channel")
                ->order("id desc")
                ->paginate(20);
                
        $lists->each(function($v, $k){
            $v['img_url']=get_upload_path($v['img_url']);
            return $v;
        });
                
        $page = $lists->render();
    	$this->assign('lists', $lists);
    	$this->assign("page", $page);
    	
    	return $this->fetch();
    }
	//分类添加	
    public function channel_add(){
        $countrys = Db::name('country')->where('type',0)->order('recommend desc')->select()->toArray();
        $this->assign('countrys', $countrys);
        return $this->fetch();
    }
	public function channel_addPost(){
		if ($this->request->isPost()) {
            $data  = $this->request->param();
            $data['title']=trim($data['title']);
			$title=$data['title'];
			if($title==""){
				$this->error("名称不能为空");
			}
            
            $isexit=Db::name('channel')->where(["title"=>$title])->find();	
            if($isexit){
                $this->error('该名称已存在');
            }
            
            $data['img_url']=trim($data['img_url']);
            if($data['img_url']==""){
				$this->error("请上传通道图片");
			}
			if(!empty($data['countrys'])){
				$data['countrys'] = json_encode($data['countrys']);
			}
            
            $data['addtime']=time();
			$id = Db::name('channel')->insertGetId($data);
            if(!$id){
                $this->error("添加失败！");
            }
            
            $this->success("添加成功！");
            
		}			
	}
	
	
	
	
	//分类编辑
    public function channel_edit(){
        $id   = $this->request->param('id', 0, 'intval');
        $data=Db::name('channel')
            ->where("id={$id}")
            ->find();
        if(!$data){
            $this->error("信息错误");
        }
        $countrys = Db::name('country')->where('type',0)->order('recommend desc')->select()->toArray();
        $this->assign('countrys', $countrys);
        if(!empty($data['countrys'])){
            //$data['countrys'] = json_decode($data['countrys'],true);
            $data['countrys'] = explode(',',$data['countrys']);
            $this->assign('countrys_id', $data['countrys']);
        }
        $this->assign('data', $data);
        return $this->fetch();			
    }
	public function channel_editPost(){
		if ($this->request->isPost()) {
            $data      = $this->request->param();
            $data['title']=trim($data['title']);
			$title=$data['title'];
			$id=$data['id'];

			if($title==""){
				$this->error("名称不能为空");
			}
            $isexit=Db::name('channel')->where([['id','<>',$id],['title','=',$title]])->find();	
            if($isexit){
                $this->error('该名称已存在');
            }
            
            $data['img_url']=trim($data['img_url']);
            if($data['img_url']==""){
				$this->error("请上传通道图片");
			}
			//print_r($data['countrys']);exit;
            if(!empty($data['countrys'])){
				//$data['countrys'] = json_encode($data['countrys']);
				$data['countrys'] = implode(',',$data['countrys']);
			}
			//var_dump($data['countrys']);exit;
			$rs = Db::name('channel')->update($data);
            if($rs===false){
                $this->error("修改失败！");
            }
            
			
			//$this->resetcache();
            $this->success("修改成功！");
		}	
	}
	
	//分类删除
	public function channel_del(){
        $id = $this->request->param('id', 0, 'intval');
        
        $rs = Db::name('channel')->where("id={$id}")->delete();
        if(!$rs){
            $this->error("删除失败！");
        }
        $this->success("删除成功！",url("channel/index"));
            
	}
	
	function methods(){
        $data = $this->request->param();
        $lists = Db::name("methods")
                ->alias('m')
                ->field("m.*")
                ->order("m.id desc")
                ->paginate(20);
        $lists->each(function($v, $k){
            $v['img_url']=get_upload_path($v['img_url']);
            $v['channel'] = Db::name("methods")
                ->field("*")
                ->where("find_in_set(id,'".$v['channel_id']."')")
                ->order("id desc")
                ->select();
            return $v;
        });
                
        $page = $lists->render();
    	$this->assign('lists', $lists);
    	$this->assign("page", $page);
    	
    	return $this->fetch();
    }
	//分类添加	
    public function methods_add(){
        $channels = Db::name("channel")
                ->order("id desc")
                ->select();
        $this->assign('channels', $channels);
        return $this->fetch();
    }
	public function methods_addPost(){
		if ($this->request->isPost()) {
            $data  = $this->request->param();
            $data['title']=trim($data['title']);
			$title=$data['title'];

			if($title==""){
				$this->error("名称不能为空");
			}
            
            $isexit=Db::name('methods')->where(["title"=>$title])->find();	
            if($isexit){
                $this->error('该名称已存在');
            }
            if(empty($data['channel_id'])){
                $this->error("请选择通道");
            }
            $data['channel_id'] = implode(',',$data['channel_id']);
            $data['img_url']=trim($data['img_url']);
            if($data['img_url']==""){
				$this->error("请上传图片");
			}
            
            $data['addtime']=time();
            
			$id = Db::name('methods')->insertGetId($data);
            if(!$id){
                $this->error("添加失败！");
            }
            
            $this->success("添加成功！");
            
		}			
	}
	
	
	
	
	//分类编辑
    public function methods_edit(){
        $id   = $this->request->param('id', 0, 'intval');
        
        $data=Db::name('methods')
            ->where("id={$id}")
            ->find();
        if(!$data){
            $this->error("信息错误");
        }
        $channels = Db::name("channel")
                ->order("id desc")
                ->select();
        $this->assign('channels', $channels);
        if(!empty($data['channel_id'])){
            $data['channel_id'] = explode(',',$data['channel_id']);
            $this->assign('channel_id', $data['channel_id']);
        }
        $this->assign('data', $data);
        return $this->fetch();			
    }
	public function methods_editPost(){
		if ($this->request->isPost()) {
            $data      = $this->request->param();
            $data['title']=trim($data['title']);
			$title=$data['title'];
			$id=$data['id'];

			if($title==""){
				$this->error("名称不能为空");
			}
            $isexit=Db::name('methods')->where([['id','<>',$id],['title','=',$title]])->find();	
            if($isexit){
                $this->error('该名称已存在');
            }
            if(empty($data['channel_id'])){
                $this->error("请选择通道");
            }
            
            $data['channel_id'] = implode(',',$data['channel_id']);
            $data['img_url']=trim($data['img_url']);
            if($data['img_url']==""){
				$this->error("请上传图片");
			}
            
			$rs = Db::name('methods')->update($data);
            if($rs===false){
                $this->error("修改失败！");
            }
            
			
			//$this->resetcache();
            $this->success("修改成功！");
		}	
	}
	
	//分类删除
	public function methods_del(){
        $id = $this->request->param('id', 0, 'intval');
        
        $rs = Db::name('methods')->where("id={$id}")->delete();
        if(!$rs){
            $this->error("删除失败！");
        }
        $this->success("删除成功！",url("channel/methods"));
            
	}
	
	function withdraw(){
        $data = $this->request->param();
        $lists = Db::name("withdraw")
                ->order("id desc")
                ->paginate(20);
                
        $lists->each(function($v, $k){
            $v['img_url']=get_upload_path($v['img_url']);
            return $v;
        });
                
        $page = $lists->render();
    	$this->assign('lists', $lists);
    	$this->assign("page", $page);
    	
    	return $this->fetch();
    }
	//分类添加	
    public function withdraw_add(){
        $countrys = Db::name('country')->where('type',0)->order('recommend desc')->select()->toArray();
        $this->assign('countrys', $countrys);
        return $this->fetch();
    }
	public function withdraw_addPost(){
		if ($this->request->isPost()) {
            $data  = $this->request->param();
            $data['title']=trim($data['title']);
			$title=$data['title'];
			$country_id = $data['country_id'];

			if($title==""){
				$this->error("名称不能为空");
			}
			
			if($country_id==""){
				$this->error("请选择国家");
			}
            $data['img_url']=trim($data['img_url']);
            if($data['img_url']==""){
				$this->error("请上传图片");
			}
            $isexit=Db::name('withdraw')->where(["title"=>$title])->find();	
            if($isexit){
                $this->error('该名称已存在');
            }
            
            
            $data['addtime']=time();
			$id = Db::name('withdraw')->insertGetId($data);
            if(!$id){
                $this->error("添加失败！");
            }
            
            $this->success("添加成功！");
            
		}			
	}
	
	
	
	
	//分类编辑
    public function withdraw_edit(){
        $id   = $this->request->param('id', 0, 'intval');
        
        $data=Db::name('withdraw')
            ->where("id={$id}")
            ->find();
        if(!$data){
            $this->error("信息错误");
        }
        $countrys = Db::name('country')->where('type',0)->order('recommend desc')->select()->toArray();
        $this->assign('countrys', $countrys);
        $this->assign('data', $data);
        return $this->fetch();			
    }
	public function withdraw_editPost(){
		if ($this->request->isPost()) {
            $data      = $this->request->param();
            $data['title']=trim($data['title']);
			$title=$data['title'];
			$id=$data['id'];

			if($title==""){
				$this->error("名称不能为空");
			}
            $isexit=Db::name('withdraw')->where([['id','<>',$id],['title','=',$title]])->find();	
            if($isexit){
                $this->error('该名称已存在');
            }
            $data['img_url']=trim($data['img_url']);
            if($data['img_url']==""){
				$this->error("请上传图片");
			}
            
			$rs = Db::name('withdraw')->update($data);
            if($rs===false){
                $this->error("修改失败！");
            }
            
			
			//$this->resetcache();
            $this->success("修改成功！");
		}	
	}
	
	//分类删除
	public function withdraw_del(){
        $id = $this->request->param('id', 0, 'intval');
        
        $rs = Db::name('withdraw')->where("id={$id}")->delete();
        if(!$rs){
            $this->error("删除失败！");
        }
        $this->success("删除成功！",url("channel/withdraw"));
            
	}
	

    
}
